﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace AccountingLibrary
{
  public class NetIncomeCollection : IEnumerable<NetIncomeClass>
    {

      public NetIncomeCollection()
      {

      }
      private DateTime frm;
      private DateTime tod;
      public NetIncomeCollection(DateTime mm ,DateTime cc,IAccountable accounts,IJournalable journals)
      {
          this.frm = mm;
          this.tod = cc;
          this.account_entities = accounts;
          this.journal_entities = journals;
      }
      //DataClasses1DataContext dc = new DataClasses1DataContext();
      IAccountable account_entities;
        public  List<NetIncomeClass> netlist = new List<NetIncomeClass>();
        public double groupvalue { get; set; }
        public IJournalable journal_entities;
        public void get_Main(string main_name)
         {
            groupvalue = 0;
            //List<account> main_accounts = (from xx in dc.accounts.AsParallel() where xx.acc_maincat == main_name && xx.acc_ref == 0 select xx).ToList();
            List<AccountBase> main_accounts = account_entities.get_account_main(main_name);
            foreach (AccountBase acco in main_accounts)
            {
                double catvalue = 0;
                get_sub(acco, 0, ref catvalue);
                groupvalue = catvalue + groupvalue;
            }
             netlist.Add(new NetIncomeClass { account_name = main_name + "مجموع", mainvalue = groupvalue });
        }
      private void get_sub(AccountBase acc, int depth, ref double sum)
        {
            lock (this)
            {
                //var mm = from dd in dc.accounts where dd.acc_ref == acc.acc_id select dd;
                List<AccountBase> mm = account_entities.get_accounts(acc.id);
                if (mm.Count() > 0)
                {
                    // write account name
                    netlist.Add(new NetIncomeClass { account_name = acc.name });
                    double subsum = 0;
                    foreach (AccountBase cc in mm)
                    {
                        get_sub(cc, depth + 1, ref subsum);

                    }
                    sum = subsum + sum;//write sum account name subsum value
                    addto(acc.name + "مجموع", subsum, depth);
                    //netlist.Add(new NetIncomeClass { account_name = acc.acc_name+"total" ,mainvalue=subsum});

                }
                else
                {
                    journalvalues(acc, ref sum, depth);
                }
            }
        }
        protected void journalvalues(AccountBase acc, ref double value,int dep )
        {
            GeneralLedgerCollection jj = new GeneralLedgerCollection(acc,journal_entities,frm,tod);
            addto(acc.name, jj.journallast, dep);
            value = jj.journallast+ value;
        }

        public IEnumerator<NetIncomeClass> GetEnumerator()
        {
         return  netlist.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return netlist.GetEnumerator();

        }
      protected void addto(string accountname ,double value ,int dep){
          if(dep ==0)netlist.Add(new NetIncomeClass { account_name = accountname, mainvalue = value });
          else if(dep==1) netlist.Add(new NetIncomeClass { account_name = accountname, value = value });
          else netlist.Add(new NetIncomeClass { account_name = accountname, subvalue = value });
      
      }

      public static NetIncomeCollection operator +(NetIncomeCollection x, NetIncomeCollection y)
      {
          NetIncomeCollection coll = new NetIncomeCollection();
          coll.netlist.AddRange(x);
          coll.netlist.AddRange(y);
          coll.groupvalue = x.groupvalue + y.groupvalue;
         
          return coll;
      }
      public static NetIncomeCollection operator -(NetIncomeCollection x, NetIncomeCollection y)
      {
          NetIncomeCollection coll = new NetIncomeCollection();
          coll.netlist.AddRange(x);
          coll.netlist.AddRange(y);
          coll.groupvalue = x.groupvalue - y.groupvalue;
          return coll;
      }
    }
}
